﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using OOLib;

namespace CopyBookmark
{
   class Program
   {
      static void Main(string[] args)
      {
         // instanciate the ServiceManager
         // the ServiceManager is a singleton and can be created only once
         // use ServiceManager.Instance to access it
         var serviceManager = new ServiceManager();

         // create the Desktop
         var desktop = serviceManager.CreateDesktop();

         // load the template
         string path = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), @"..\..\..\Templates\CopyBookmarkTemplate.ott"));
         TextDocument textDoc = desktop.LoadComponentFromFile<TextDocument>(path);

         // get the target range from bookmark
         var targetBookmark = textDoc.Bookmarks["Target"];
         // the traget range is the anchor of the bookmark
         TextRange targetRange = targetBookmark.GetAnchor();

         // wait for input
         Console.WriteLine("Press '1' to paste before target");
         Console.WriteLine("Press '2' to paste after target");
         Console.WriteLine("Press any other key to replace target");
         Console.WriteLine("Make your selection ...");
         var ki = Console.ReadKey();

         if (ki.KeyChar == '1')
         {
            // get the start of the targetRange to insert before it
            targetRange = targetRange.GetStart();
         }
         else if (ki.KeyChar == '2')
         {
            // get the end of the targetRange to insert after it
            targetRange = targetRange.GetEnd();
         }
         // on any other key the complete text range will be raplaced

         Console.WriteLine();

         // copy source to target bookmark
         textDoc.CopyBookmarkContent("Source", targetRange);

         // wait for input
         Console.WriteLine("Press key to continue ...");
         Console.ReadKey();

         // close the Desktop
         desktop.Terminate();

         // cleanup
         textDoc = null;
         desktop = null;
         serviceManager = null;
      }
   }
}
